//app.js
const defaultTime = {
  defaultWorkTime: 25,
  defaultRestTime: 5
}

App({
  // 获取UIinfo
  initUiGlobal() {
    wx.getSystemInfo({
      success: e => {
        const { statusBarHeight: StatusBar, screenHeight, windowWidth } = e
        this.globalData.StatusBar = StatusBar
        this.globalData.screenHeight = screenHeight
        this.globalData.windowWidth = windowWidth
        const custom = wx.getMenuButtonBoundingClientRect()
        if (custom) {
          this.globalData.Custom = custom
          this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight
        } else {
          this.globalData.CustomBar = StatusBar + 50
        }
      }
    })
  },

  //初始化环境,做一个开发环境和测试环境/后期
  initEnv() {
    const env = 'cloud-environment-5e1r8y90a6ba7e'
    wx.cloud.init({
      env
    })
    this.globalData.env = env
  },

  onLaunch(){
    let workTime = this.globalData.workTime
    let restTime = this.globalData.restTime
    if (!workTime) {
      this.globalData.workTime = defaultTime.defaultWorkTime
    }
    if (!restTime) {
      this.globalData.restTime = defaultTime.defaultRestTime
    }
    this.initUiGlobal()
    this.initEnv()
  },

  globalData: {
    // 用户信息
    userInfo: {},
    openid: null,
    hasUserInfo: false,
    requestUserInfo: [], // 好友信息
    // 番茄时间
    workTime: null,
    restTime: null,
    // 专注记录
    logs: [],
    totalFocusTime: null,
    totalFocusNumber: null,
    todayLogs: [],
    todayFocusTime: null,
    todayFocusNumber: null,
    // 待办
    todoLists: [],
    isTodo: 0,
    todoIndex: -1,
    // 界面
    StatusBar: null,
    Custom: null,
    CustomBar: null,
    screenHeight: null,
    windowWidth: null,
    // 环境
    env: null
  },

  // getApp().watch("totalFocusTime", (res) => {this.setData({totalFocusTime: res})}, this);
  watch: function(dataName, callback, that) {
    let obj = this.globalData // 拿到全局变量
    Object.defineProperty(obj, dataName, {
      configurable: true, //可配置
      enumerable: true, //可枚举
      set: value => { //当监听到的函数被设置新值的时候执行
        this['temp' + dataName] = value;
        callback.call(that, value) //调用回调函数。传入新值
      },
      get: () => {
        return this['temp' + dataName]
      }
    })
  }
})
